var app = getApp(),
  $ = require("../../utils/httpHelper.js"),
  WxNotificationCenter = require("../../WxNotificationCenter/WxNotificationCenter.js")//通知传参
var wxParse = require("../../wxParse/wxParse.js")
Page({
  data: {
    objarr: [],
    is_fw:false,
    height: '',
    width: '',
    good_info:{},
    currentTab: 0,
    goodsinfo:{},
    isfav:true,
    goods_id:0,
    status:'',
    yhq_status:true,
    goods_gg_status: true,
    time_unit:'',
    num:1,
    speclist:[],
    speclist_array: [],
    coupon_list:[],
    activeid:'',
    sell_p:'',
    sku_id:'',
    add_cart:false,
    goods_type:'good',
    selfw_id:'',
    selfw_name:'请选择服务',
    price:'',
    market_price:'',
    stock:'',
    sell_total_num:'',
    shop_info:{},
    endtime:'',
    day:'',
    hour:'',
    min:'',
    sec:''
  },
  onShareAppMessage: function (res) {
    app.shareApp(res)
  },
  onLoad: function (options) {
    console.log("onload..")

    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          height: res.windowHeight,
          width: res.windowWidth
        })
      }
    });
    
    if (!app.check_empty(options.id)){
      var rd_session = wx.getStorageSync('rd_session');
      this.setData({
        goods_id: options.id,
        goods_type: options.status,
      })
      let par = {
        good_id: options.id, 
        rd_session: rd_session, 
        shop_id: wx.getStorageSync('shop_id')
      }
      if (!app.check_empty(options.status) && options.status == 'miao'){
        $.httpPost("good_detail_miao", par, (data, err) => {
          let arr = []
          if (data.data.spec_list.length == 0) {
            arr = data.data.spec_list
          } else {
            arr = data.data.spec_list[0].value_list
          }
          this.setData({
            shop_info: data.data.shop_info,
            good_info: data.data.good_info,
            price: data.data.good_info.sell_price,
            market_price: data.data.good_info.market_price,
            stock: data.data.good_info.stock,
            sell_total_num: data.data.good_info.sell_total_num,
            speclist: arr,
            isfav: data.data.good_info.is_favorite,
            endtime: data.data.good_info.end_time
          });
          if (data.data.good_info.content) {
            wxParse.wxParse("about", "html", data.data.good_info.content, this)
          }
          this.getCountDown(this.data.endtime)
        })
      } else if (!app.check_empty(options.status) && options.status == 'good'){
        $.httpPost("good_detail", par, (data, err) => {
          let arr = []
          if (data.data.spec_list.length == 0) {
            arr = data.data.spec_list
          } else {
            arr = data.data.spec_list[0].value_list
          }
          this.setData({
            shop_info: data.data.shop_info,
            good_info: data.data.good_info,
            price: data.data.good_info.sell_price,
            market_price: data.data.good_info.market_price,
            stock: data.data.good_info.stock,
            sell_total_num: data.data.good_info.sell_total_num,
            speclist: arr,
            isfav: data.data.good_info.is_favorite,
          });
          if (data.data.good_info.content) {
            wxParse.wxParse("about", "html", data.data.good_info.content, this)
          }
        })
      } else if (!app.check_empty(options.status) && options.status == 'jifen') {
        $.httpPost("good_detail_jifen", par, (data, err) => {
          this.setData({
            shop_info: data.data.shop_info,
            good_info: data.data.data,
            price: data.data.data.sell_price,
            market_price: data.data.data.market_price,
            stock: data.data.data.stock,
            sell_total_num: data.data.data.sell_total_num,
            speclist: [],
          });
          if (data.data.data.content) {
            wxParse.wxParse("about", "html", data.data.data.content, this)
          }
        })
      }
      
      
    }
    
  },
  getCountDown(timestamp) {
    let that = this
    setInterval(function () {
      var nowTime = new Date();
      var endTime = new Date(timestamp * 1000);
      var t = endTime.getTime() - nowTime.getTime();
      var day = Math.floor(t/1000/60/60/24);
      var hour = Math.floor(t / 1000 / 60 / 60 % 24);
      var min = Math.floor(t / 1000 / 60 % 60);
      var sec = Math.floor(t / 1000 % 60);
      if (day < 10) {
        day = "0" + day;
      }
      if (hour < 10) {
        hour = "0" + hour;
      }
      if (min < 10) {
        min = "0" + min;
      }
      if (sec < 10) {
        sec = "0" + sec;
      }
      that.setData({
        day:day,
        hour:hour,
        min:min,
        sec:sec
      })
      var countDownTime = hour + ":" + min + ":" + sec;
    }, 1000);
  },
  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value);
    var arr = e.detail.value.split('-')
      this.setData({
        selfw_id:arr[0],
        selfw_name:arr[1]
      })
    
  },
  usefu(){
    let par = {
      rd_session: wx.getStorageSync('rd_session'),
      good_id: this.data.goods_id, 
      sku_ids: this.data.selfw_id
    }
    $.httpPost("get_sku", par, (data, err) => {
      this.setData({
        price: data.data.price,
        market_price: data.data.market_price,
        stock: data.data.stock,
        sell_total_num: data.data.sell_total_num,
        is_fw: false
      });
    })
  },
  seleFw(){
    this.setData({
      is_fw:true
    })
  },
  hiddenfw() {
    this.setData({
      is_fw: false
    })
  },
  topingjia(){
    wx.navigateTo({
      url: '/pages/goods/pingjia?good_id=' + this.data.goods_id
    })
  },
  clicktab(e) {
    console.log(e.target.dataset.tab)
    let tab = e.target.dataset.tab
    if (this.data.currentTab != tab) {
      this.setData({
        currentTab: tab
      })
    }
  },
  favorite(){
    if(this.data.isfav){
      var rd_session = wx.getStorageSync('rd_session');
      $.httpPost("delete_favorite_good", { good_id: this.data.goods_id, rd_session: rd_session }, (data, err) => {
        this.setData({
          isfav: data.favorite
        })
      })
    }else{
      var rd_session = wx.getStorageSync('rd_session');
      $.httpPost("add_favorite_good", { good_id: this.data.goods_id, rd_session: rd_session,types:1}, (data, err) => {
        this.setData({
          isfav: data.favorite
        })
      })
    }
    
  },
  callphone(e){
    console.log(e)
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone
    })
  },
  yhqShow(){
    $.httpPost("coupon_list", {}, (data, err) => {
      this.setData({
        yhq_status: false,
        coupon_list:data.data.list
      })
    })
    
  },
  getYhq(e){
    console.log(e)
    var rd_session = wx.getStorageSync('rd_session');
    $.httpPost("get_coupon", { coupon_id: e.currentTarget.dataset.id, rd_session: rd_session}, (data, err) => {
      wx.showToast({
        title: data.result,
      })
      this.yhqHide()
    })
  },
  yhqHide(){
    this.setData({
      yhq_status: true
    })
  },
  canlGG(){
    this.setData({
      goods_gg_status: true
    })
  },
  goodsGG() {
    // if (this.data.selfw_id == '' && this.data.speclist.length != 0){
    //   wx.showToast({
    //     title: '先选择服务',
    //   })
    //   return
    // }
    this.setData({
      goods_gg_status: false,
    })
  },
  addCart(){
    this.setData({
      goods_gg_status: false,
      add_cart:true
    })
  },
  //待付款页面
  obligation(){
      var rd_session = wx.getStorageSync('rd_session');
      if(this.data.add_cart){
        let par = {}
        if (this.data.speclist.length == 0) {
          par = {
            good_id: this.data.goods_id, 
            total_num: this.data.num, 
            rd_session: rd_session
          }
        } else {
          if (!app.check_empty(this.data.sku_id)) {
            par = {
              good_id: this.data.goods_id,
              total_num: this.data.num,
              rd_session: rd_session,
              sku_id:this.data.sku_id
            }
          } else {
            wx.showToast({
              title: '先选择服务',
            })
            return
          }
        }
        $.httpPost("add_cart", par,(data,err) => {
          wx.showToast({
            title: data.message,
            duration: 2000,
          })
          this.setData({
            add_cart: false
          })
        })
      }else{
        let par = ''
        if (this.data.speclist.length == 0){
          par = `num=${this.data.num}&good_id=${this.data.goods_id}`
        }else{
          if (!app.check_empty(this.data.sku_id)){
            par = `num=${this.data.num}&good_id=${this.data.goods_id}&sku_id=${this.data.sku_id}`
          }else{
            wx.showToast({
              title: '先选择服务',
            })
            return
          }
        }
        wx.navigateTo({
          url: '/pages/obligation/obligation?' + par
        })
      }
      this.setData({
        goods_gg_status: true,
      })
  },
  todh(){
    $.httpPost("get_good_jifen", { good_id: this.data.goods_id, rd_session: wx.getStorageSync('rd_session') }, (data, err) => {
      wx.showToast({
        title: data.message,
      })
      setTimeout(function(){
        if (data.result == 'success') {
          wx.switchTab({
            url: '/pages/mine/mine',
          })
        }
      },1000)
      
    })
  },
  sub(){
    console.log(this.data.num)
    if(this.data.num == 1) return;
    this.setData({
      num:this.data.num-1
    })
  },
  add(){
    this.setData({
      num: this.data.num+1
    })
  },
  seleAct(e){
    let active_id = e.target.dataset.id
    let active_index = e.target.dataset.key
    let speclist = this.data.speclist
    let sku_id = ''
    for(let index in speclist){
      speclist[index]['active'] = false
      if (index == active_index) {
        speclist[index]['active'] = true;
        sku_id = speclist[index].id
      }
    }
      
    this.setData({
      speclist: speclist
    })
    $.httpPost("get_sku", { sku_ids: sku_id, good_id: this.data.goods_id, rd_session: wx.getStorageSync('rd_session')}, (data, err) => {
      this.setData({
        price: data.data.price,
        market_price: data.data.market_price,
        stock: data.data.stock,
        sell_total_num: data.data.sell_total_num,
        sku_id : data.data.id
      })
    })
  }
});